package com.neu.questionnaireplus.service.impl;

import com.neu.questionnaireplus.dao.TenementDao;
import com.neu.questionnaireplus.dao.UserDao;
import com.neu.questionnaireplus.entity.Tenement;
import com.neu.questionnaireplus.entity.User;
import com.neu.questionnaireplus.service.TenementService;
import com.neu.questionnaireplus.service.UserService;
import com.neu.questionnaireplus.utils.common.CommonUtils;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

@Validated
@Service
public class TenementServiceImpl implements TenementService {

  @Autowired
  private TenementDao tenementDao;

  @Autowired
  private UserDao userDao;

  @Autowired
  private UserService userService;

  @Override
  public Tenement queryTenementByUsername(String username) {
    return tenementDao.queryTenementByUsername(username);
  }

  @Override
  public List<Tenement> queryTenement() {
    return tenementDao.queryTenement();
  }

  @Override
  public List<Tenement> queryExistTenement() {
    return tenementDao.queryExistTenement();
  }

  @Override
  public List<Tenement> selectTenementList(Tenement tenement) {
    return tenementDao.selectTenementList(tenement);
  }

  @Override
  public Tenement queryTenementByID(String id) {
    return tenementDao.queryTenementByID(id);
  }

  @Transactional
  @Override
  public boolean insertTenement(Tenement tenement) {
    if (tenement != null && !tenement.getId().equals("")){
      try {
        String username = tenement.getUsername();
        String telnumber = tenement.getTelnumber();
        if (tenementDao.queryTenementByPhone(telnumber) != null){
          throw new RuntimeException("租户电话已存在");
        } else if (tenementDao.queryTenementByUsername(username) != null) {
          throw new RuntimeException("用户名已存在");
        } else {
          int i = tenementDao.insertTenement(tenement);
          if (i == 1) return true;
          else throw new RuntimeException("插入租户失败！！" + tenement);
        }
      }catch (Exception e){
        throw new RuntimeException("插入租户失败！！" + e.getMessage());
      }
    }else {
      throw new RuntimeException("插入租户失败,租户id不能为空");
    }
  }
  @Transactional
  @Override
  public boolean updateTenement(Tenement tenement) {
    if (tenement != null && !tenement.getId().equals("")){
      try {
        String username = tenement.getUsername();
        String telnumber = tenement.getTelnumber();
        Tenement tenement1 = tenementDao.queryTenementByPhone(telnumber);
        Tenement tenement2 = tenementDao.queryTenementByUsername(username);
        if ((tenement1 != null || tenement2 != null) && ( (tenement1 != null && !tenement.getId().equals(tenement1.getId())) || (tenement2 != null && !tenement.getId().equals(tenement2.getId()))) ){
          throw new RuntimeException("修改的用户名或手机号与其他用户有冲突");
        }else {
          int i = tenementDao.updateTenement(tenement);
          if (i == 1) return true;
          else throw new RuntimeException("更新租户失败！！" + tenement);
        }
      }catch (Exception e){
        throw new RuntimeException("更新租户失败！！" + e.getMessage());
      }
    }else {
      throw new RuntimeException("更新租户失败,租户id不能为空");
    }
  }

  @Transactional
  @Override
  public boolean deleteTenement(String id) {
    if (id != null && !id.equals("")){
      try {
        int i = tenementDao.deleteTenement(id);
        if (i == 1) return true;
        else throw new RuntimeException("删除租户失败！！" + id);
      }catch (Exception e){
        throw new RuntimeException("删除租户失败！！" + e.getMessage());
      }
    }else {
      throw new RuntimeException("删除租户失败,租户id不能为空");
    }
  }

  @Override
  public Tenement queryTenementByPhone( String telnumber) {
    if (telnumber != null && !telnumber.equals("")){
      Tenement t = null;
      t = tenementDao.queryTenementByPhone(telnumber);
      if (t == null){
        throw new RuntimeException("未查询到该电话号码的租户");
      }else {
        return t;
      }
    }else {
      throw new RuntimeException("电话号码为空！！");
    }
  }

  @Override
  public boolean loginByPassword(String username, String password) {
    Tenement t = null;
    t = tenementDao.queryTenementByUsername(username);
    if (t == null){
      throw new RuntimeException("用户名不存在");
    }else {
      if (t.getPassword().equals(password)){
        if(t.getDelFlag() == 1 || t.getStatus() == 1){
          throw new RuntimeException("账户被冻结或已经被删除请联系管理员");
        }else {
          t.setLastLoginDate(CommonUtils.getDateByDateString(CommonUtils.getFormatDateTimeNow()));
          boolean res = updateTenement(t);
          return res;
        }
      }else {
        throw new RuntimeException("密码错误");
      }
    }
  }

  @Override
  public boolean checkSecurityQuestion( String username, String answer) {
    Tenement t = null;
    t = tenementDao.queryTenementByUsername(username);
    if (t == null){
      throw new RuntimeException("用户名不存在");
    }else {
      if (t.getSecurityQuestionAnswer().equals(answer)){
        return true;
      }else {
        throw new RuntimeException("密保验证错误");
      }
    }
  }

  @Override
  public boolean hasDataRelation( String id) {
    //查询该租户下的所属用户
    Tenement tenement = tenementDao.queryTenementByID(id);
    List<User> list = userDao.queryUserByTenementName(tenement.getUsername());
    //如果所属租户有数据关联 则租户具有数据关联
    for (User user : list){
      if(userService.hasDataRelation(user.getId()))
        return true;
    }
    return false;
  }

  @Override
  public List<Tenement> queryTenementByName(String username) {
    return tenementDao.queryTenementByName(username);
  }

  @Transactional
  @Override
  public boolean forbiddenTenement( String id) {
    Tenement t = null;
    t = tenementDao.queryTenementByID(id);
    if (t == null){
      throw new RuntimeException("租户不存在");
    }else {
      if (t.getStatus() == 1){//表示已经被冻结
        throw new RuntimeException("租户已经被冻结不可重复冻结");
      }else {
        t.setStatus(1);
        int flag = tenementDao.updateTenement(t);
        if (flag == 1){
          return true;
        }else {
         throw new RuntimeException("冻结用户失败");
        }
      }
    }
  }

  @Transactional
  @Override
  public boolean freeTenement( String id) {
    Tenement t = null;
    t = tenementDao.queryTenementByID(id);
    if (t == null){
      throw new RuntimeException("租户不存在");
    }else {
      if (t.getStatus() == 0){//表示未被冻结
        throw new RuntimeException("租户未处于冻结状态无需解冻租户");
      }else {
        t.setStatus(0);
        int flag = tenementDao.updateTenement(t);
        if (flag == 1){
          return true;
        }else {
          throw new RuntimeException("解冻用户失败");
        }
      }
    }
  }

  @Transactional
  @Override
  public boolean modifyTenementInfo(Tenement tenement) {
    String id = tenement.getId();
    if (tenementDao.queryTenementByID(id) != null){
      if (hasDataRelation(id)){
        throw new RuntimeException("该租户正在服务一些群组，不可修改信息");
      }else {
        int flag = tenementDao.updateTenement(tenement);
        if (flag == 1){
          return true;
        }else {
          throw new RuntimeException("修改失败");
        }
      }
    }else {
      throw new RuntimeException("租户不存在");
    }
  }

  @Transactional
  @Override
  public boolean deleteTenementInfo(Tenement tenement) {
    String id = tenement.getId();
    if (tenementDao.queryTenementByID(id) != null){
      if (hasDataRelation(id)){
        throw new RuntimeException("该租户正在服务一些群组，不可删除该租户");
      }else {
        tenement.setDelFlag(1);
        int flag = tenementDao.updateTenement(tenement);
        if (flag == 1){
          return true;
        }else {
          throw new RuntimeException("删除失败");
        }
      }
    }else {
      throw new RuntimeException("租户不存在");
    }
  }

  @Override
  public boolean loginByPhone(String phone, String inputStr, String realStr) {
    Tenement tenement = tenementDao.queryTenementByPhone(phone);
    if (tenement != null){
      if (tenement.getStatus() == 1 || tenement.getDelFlag() == 1){
        throw new RuntimeException("该用户被冻结或被删除");
      }else {
        if (inputStr.equals(realStr)){
          return true;
        }else {
          throw new RuntimeException("手机验证码不正确");
        }
      }
    }else {
      throw new RuntimeException("该手机号下的租户不存在！");
    }
  }

  @Override
  public boolean checkRegisterStr(String phone, String inputStr, String realStr) {
    Tenement tenement = tenementDao.queryTenementByPhone(phone);
    if (tenement == null){
//      System.out.println(inputStr + " " + realStr);
      if (inputStr.equals(realStr)){
        return true;
      }else {
        throw new RuntimeException("手机验证码不正确");
      }
    }else {
      throw new RuntimeException("已经存在该租户，同一个手机号不能重复注册");
    }
  }

  @Override
  public boolean checkResetPwdStr(String phone, String inputStr, String realStr) {
    Tenement tenement = tenementDao.queryTenementByPhone(phone);
    if (tenement != null){
      if (inputStr.equals(realStr)){
        return true;
      }else {
        throw new RuntimeException("手机验证码不正确");
      }
    }else {
      throw new RuntimeException("该手机号下的租户不存在！");
    }
  }
}
